/*
 * @Descripttion: @能力的文本加解密
 * @version: 
 * @Author: 川杨
 * @Date: 2022-02-25 10:21:52
 * @LastEditors: 川杨
 * @LastEditTime: 2022-03-02 10:47:47
 */

/*
 * @param {*} atUsers @人的数组
 * @param {*} content 文本内容
 * 加密text，最终效果如 "你好呀${userId:'111',avatar:'sssss.com',name:'@张三'}$"
 */
export function enCodeAtText(content, atUsers){
  if(!atUsers || atUsers.length === 0) return content;
  atUsers.forEach(item=>{
    let matchText = querysour(`@${item.name || item.displayName} `)
    // matchText = matchText.replace(new RegExp(/\*/, 'g'), '\\*')
    let jsonString = `$${JSON.stringify(item)}`
    content = content.replace(new RegExp(matchText, 'g'), jsonString)
  })

  return content
}

/*
 * @param {*} content 文本内容
 * 解密text，转成html，最终效果如 "你好呀<span data="{userId:'111',avatar:'sssss.com',name:'@张三'}">@张三</span>"
 */
export function atTextToHtml(content){
  if(!content) return '';
  content = content.replace(new RegExp('&quot;', 'g'), '"') // 兼容后端对于引号的转换
  let matchArr = content.match(/\${.*?\}/g)
  if(!matchArr)return content;
  matchArr = new Set(matchArr)
  matchArr.forEach(item=>{
    if(item.indexOf('userId') > -1){
      let htmlString = `<span class="user-card-triggle" data='${item.substring(1)}'>@${JSON.parse(item.substring(1)).name || JSON.parse(item.substring(1)).displayName || ''} </span>`
      item = querysour(item)
      content = content.replace(new RegExp(item, 'g'), htmlString)
    }
  })
  return content
}

export function atTextToHtml1(content){
  if(!content) return '';
  content = content.replace(new RegExp('&quot;', 'g'), '"') // 兼容后端对于引号的转换
  let matchArr = content.match(/\${.*?\}/g)
  if(!matchArr)return content;
  matchArr = new Set(matchArr)
  matchArr.forEach(item=>{
    if(item.indexOf('userId') > -1){
      let htmlString = `@${JSON.parse(item.substring(1)).name || JSON.parse(item.substring(1)).displayName || ''}`
      item = querysour(item)
      content = content.replace(new RegExp(`${item}`, 'g'), htmlString)
    }
  })
  return content
}

// 用来解析文本，返回原先@的人和内容
export function atTextToUsers(content){
  content = content.replace(new RegExp('&quot;', 'g'), '"') // 兼容后端对于引号的转换
  let matchArr = content.match(/\${.*?\}/g)
  if(!matchArr)return {content, users:[]};
  matchArr = new Set(matchArr)
  let users = []
  matchArr.forEach(item=>{
    if(item.indexOf('userId') > -1){
      let htmlString = `@${JSON.parse(item.substring(1)).name || JSON.parse(item.substring(1)).displayName || ''} `
      users.push({userId:JSON.parse(item.substring(1)).userId, name:JSON.parse(item.substring(1)).name || JSON.parse(item.substring(1)).displayName, displayName:JSON.parse(item.substring(1)).name || JSON.parse(item.substring(1)).displayName})
      item = querysour(item)
      content = content.replace(new RegExp(`${item}`, 'g'), htmlString)
    }
  })
  return {content, users}
}

function querysour(e){
  e = e.replace(new RegExp(/\\/, 'g'), '\\\\')
  e = e.replace(new RegExp(/\*/, 'g'), '\\*')
  e = e.replace(new RegExp(/\^/, 'g'), '\\^')
  e = e.replace(new RegExp(/\$/, 'g'), '\\$')
  e = e.replace(new RegExp(/\?/, 'g'), '\\?')
  e = e.replace(new RegExp(/\{/, 'g'), '\\{')
  e = e.replace(new RegExp(/\}/, 'g'), '\\}')
  e = e.replace(new RegExp(/\(/, 'g'), '\\(')
  e = e.replace(new RegExp(/\)/, 'g'), '\\)')
  return e
}

// 过滤人员
export function getReceivers(remark, users){
  let receivers = []
  users.forEach(item=>{
    if(remark.indexOf(`${item.displayName} `) > -1){
      receivers.push(item.userId)
    }
  })
  return receivers.join(',')
}

